/// API状态枚举
enum ApiStatus {
  /// 成功
  success,
  
  /// 失败
  failed,
  
  /// 未授权
  unauthorized,
  
  /// 禁止访问
  forbidden,
  
  /// 无效请求
  badRequest,
  
  /// 未找到
  notFound,
  
  /// 服务器错误
  serverError,
  
  /// 请求超时
  timeout,
  
  /// 无网络连接
  noConnection,
  
  /// 取消请求
  cancelled,
  
  /// JSON解析错误
  parseError,
}

/// API状态扩展方法
extension ApiStatusExtension on ApiStatus {
  /// 是否为成功状态
  bool get isSuccess => this == ApiStatus.success;
  
  /// 是否为错误状态
  bool get isError => this != ApiStatus.success;
  
  /// 是否为认证错误
  bool get isAuthError => this == ApiStatus.unauthorized || this == ApiStatus.forbidden;
  
  /// 是否为网络错误
  bool get isNetworkError => this == ApiStatus.noConnection || this == ApiStatus.timeout;
  
  /// 是否为服务器错误
  bool get isServerError => this == ApiStatus.serverError;
  
  /// 获取错误代码
  int get errorCode {
    switch (this) {
      case ApiStatus.success:
        return 200;
      case ApiStatus.badRequest:
        return 400;
      case ApiStatus.unauthorized:
        return 401;
      case ApiStatus.forbidden:
        return 403;
      case ApiStatus.notFound:
        return 404;
      case ApiStatus.serverError:
        return 500;
      case ApiStatus.timeout:
        return 408;
      case ApiStatus.noConnection:
        return -1;
      case ApiStatus.cancelled:
        return -2;
      case ApiStatus.parseError:
        return -3;
      case ApiStatus.failed:
        return -4;
    }
  }
} 